package cn.iocoder.yudao.module.blog.convert.post;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.controller.app.post.vo.AppBlogPostRespVO;
import cn.iocoder.yudao.module.blog.convert.base.BlogConvert;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.blog.controller.admin.post.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.post.BlogPostDO;

/**
 * 帖子 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface BlogPostConvert {

    BlogPostConvert INSTANCE = Mappers.getMapper(BlogPostConvert.class);

    BlogPostDO convert(BlogPostCreateReqVO bean);

    BlogPostDO convert(BlogPostUpdateReqVO bean);

    BlogPostRespVO convert(BlogPostDO bean);

    default AppBlogPostRespVO convertApp(BlogPostDO bean, MemberUserRespDTO memberUserRespDTO){
        if ( bean == null ) {
            return null;
        }

        AppBlogPostRespVO appBlogPostRespVO = new AppBlogPostRespVO();

        appBlogPostRespVO.setMemberId( bean.getMemberId() );
        appBlogPostRespVO.setModuleId( bean.getModuleId() );
        appBlogPostRespVO.setTitle( bean.getTitle() );
        appBlogPostRespVO.setContent( bean.getContent() );
        appBlogPostRespVO.setStatus( bean.getStatus() );
        appBlogPostRespVO.setId( bean.getId() );
        appBlogPostRespVO.setCreateTime( bean.getCreateTime() );
        appBlogPostRespVO.setMember(BlogConvert.INSTANCE.covnertMember(memberUserRespDTO));
        appBlogPostRespVO.setIsTop( bean.getIsTop() );
        appBlogPostRespVO.setLikeNum( bean.getLikeNum() );
        appBlogPostRespVO.setCommentNum( bean.getCommentNum() );


        return appBlogPostRespVO;
    }

    List<BlogPostRespVO> convertList(List<BlogPostDO> list);

    PageResult<BlogPostRespVO> convertPage(PageResult<BlogPostDO> page);

    default PageResult<AppBlogPostRespVO> convertAppPage(PageResult<BlogPostDO> page, Map<Long, MemberUserRespDTO> memberMap){
        if ( page == null ) {
            return null;
        }

        PageResult<AppBlogPostRespVO> pageResult = new PageResult<AppBlogPostRespVO>();
        List<AppBlogPostRespVO> list = convertAppList(page.getList(), memberMap);

        pageResult.setList(list);
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }

    default List<AppBlogPostRespVO> convertAppList(List<BlogPostDO> blogPostDOList, Map<Long, MemberUserRespDTO> memberMap){
        List<AppBlogPostRespVO> list = new ArrayList<>();
        blogPostDOList.forEach(blogPostDO -> {
            list.add(convertApp(blogPostDO, memberMap.get(blogPostDO.getMemberId())));
        });

        return list;
    }

    List<BlogPostExcelVO> convertList02(List<BlogPostDO> list);

}
